<template>
  <!-- 选择排班并设置诊室 -->
  <el-dialog title="设置排班" :visible="value" width="400px" custom-class="dialog-box" @close="onClose" @open="onOpen" :close-on-click-modal="false" :append-to-body="true">
    <el-form @submit.native.prevent="onSubmit" label-width="100px" ref="form" :model="form" :rules="rules">
      <el-form-item label="排班时间：" prop="typeId">
        <el-select v-model="form.typeId" clearable>
          <el-option v-for="(item,index) in panBanTypeArray" :key="index" :value="item.id" :label="item.title"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="诊室：" prop="roomId">
        <el-select v-model="form.roomId" clearable>
          <el-option v-for="(item,index) in roomArray" :key="index" :value="item.roomId" :label="item.roomName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否线上预约">
        <el-radio-group v-model="form.isOnlineRegister">
          <el-radio :label=1>是</el-radio>
          <el-radio :label=0>否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: ["dialogPaiBanObj", "value"],
  data() {
    return {
      btnLoading: false,
      form: {
        roomId: "",
        timeStr: "",
        type: 1,
        isOnlineRegister: 1,
        id: null,
        typeId: "",
        doctorId: "",
        deptId: "",
        hospitalId: ""
      },
      rules: {
        typeId: [{ required: true, message: "此项必填", trigger: "change" }],
        roomId: [{ required: true, message: "此项必填", trigger: "change" }]
      },
      panBanTypeArray: [], //排班类型
      roomArray: [] //诊室
    };
  },
  mounted() {
    this.form.hospitalId = this.adminUser.hpId;
  },
  methods: {
    onSubmit() {
      let th = this;
      let fn = function() {
        let infos = [];
        let j = {};
        Object.assign(j, th.form);
        j.roomId = th.form.roomId ? th.form.roomId : null;
        j.typeId = th.form.typeId ? th.form.typeId : null;
        infos.push(j);
        let json = {
          infosJson: JSON.stringify(infos)
        };
        th.btnLoading = true;
        th.$api.setScheduleSchedule(json).then(res => {
          if (res.executed) {
            th.$message.success("操作成功");
            th.$emit("success");
            th.onClose();
          } else {
            th.$message.error(res.message);
          }
          th.btnLoading = false;
        }).catch(res=>{
          th.btnLoading=false;
        });
      };
      th.$refs["form"].validate(v => {
        if (v) {
          fn();
        }
      });
    },
    //获取诊室
    getPaiBanRoom() {
      let th = this;
      let j = {
        deptId: th.form.deptId,
        hpId: th.form.hospitalId,
        status:1
      };
      th.$api.findHpDiagnosisRoomAll(j).then(res => {
        th.roomArray = res.dataBody.list;
      });
    },
    //获取排班类型
    getPaiBanType() {
      let th = this;
      let j = {
        deptId: th.form.deptId,
        hpId: th.form.hospitalId,
        status:1,
      };
      th.$api.getScheduleTypeList(j).then(res => {
        th.panBanTypeArray = res.dataBody.list;
      });
    },
    onOpen() {
      let th = this;
      th.$nextTick(function() {
        th.form.deptId = th.dialogPaiBanObj.deptId;
        th.form.doctorId = th.dialogPaiBanObj.doctorId;
        th.form.timeStr = th.dialogPaiBanObj.timeStr;
        th.form.id = th.dialogPaiBanObj.obj.id;
        //这里如果一个医生在两个科室开启下面代码会显示错误
        // th.form.typeId = th.dialogPaiBanObj.obj.typeId
        //   ? th.dialogPaiBanObj.obj.typeId
        //   : "";
        // th.form.roomId = th.dialogPaiBanObj.obj.roomId
        //   ? th.dialogPaiBanObj.obj.roomId
        //   : "";
        th.getPaiBanType();
        th.getPaiBanRoom();
      });
    },
    onClose() {
      let th = this;
      th.$refs["form"].resetFields();
      th.$emit("input", false);
    }
  }
};
</script>
